package com.admin.controller;

import com.admin.entity.Apply;
import com.admin.service.ApplyService;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.module.office.entity.SysOffice;
import com.admin.sys.base.module.office.service.SysOfficeService;
import com.admin.sys.utils.chart.ChartUtils;
import com.admin.sys.utils.chart.data.DataLine;
import com.admin.sys.utils.chart.data.DataPie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *【图表统计】页面接口
 */
@RestController
@RequestMapping("/admin/count")
public class CountController extends BaseController {

    @Autowired
    private ApplyService applyService;
    @Autowired
    private SysOfficeService sysOfficeService;

    /**
     * 【类别信息】数据统计
     * @param apply
     * @return
     */
    @RequestMapping("/getCount")
    public ResultInfo getCount(Apply apply) {
        Map<String, Object> data = new HashMap<>();
        List<Apply> list = applyService.getList(apply);
        DataLine line = ChartUtils.createLine();
        DataPie pie = ChartUtils.createPie();
        //数据分组统计
        Map<String, List<Apply>> collect = list.stream().collect(Collectors.groupingBy(Apply::getOfficeId));
        for (Map.Entry<String, List<Apply>> entry : collect.entrySet()) {
            SysOffice office = sysOfficeService.get(entry.getKey()+"");
            String name = office.getName();
            //柱状图
            line.getxData().add(name);
            line.getyData().add(entry.getValue().size() + "");
            //饼状图
            DataPie.Item item = pie.createItem();
            item.setName(name);
            item.setValue(entry.getValue().size() + "");
            pie.getData().add(item);
        }
        data.put("line", line);
        data.put("pie", pie);
        return ResultInfo.ok("获取列表成功", data);
    }

}



